/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package structure;

import java.util.ArrayList;

/**
 *
 * @author Gary Furlong
 */
public class Variable{
    private int id;
    private int numberOfStates;
    private boolean root = false;
    private ArrayList<State> states = new ArrayList();
    private ArrayList<Variable> parents = new ArrayList();
    private ArrayList<Variable> children = new ArrayList();

    public Variable(){
    }

    public Variable(int varId, int nos, boolean root){
        id = varId;
        numberOfStates = nos;
        for(int i=1;i<nos+1;i++){
            addState(new State(this,i));
        }
        this.root = root;
    }

    public boolean isRoot(){
        return root;
    }

    public void setRoot(boolean root){
        this.root = root;
    }

    public void setStates(ArrayList<State> stateList){
        states = stateList;
    }

    public void setNumberOfStates(int no){
        numberOfStates = no;
    }

    public ArrayList<State> getStates(){
        return states;
    }

    public State getStateById(int sid){
        for(State s : states){
            if (s.getStateNumber() == sid){
                return s;
            }
        }
        throw new IndexOutOfBoundsException("There is no variable with the inputted ID");
    }
    
    public int getNumberOfStates(){
        return numberOfStates;
    }

    public int getId(){
        return id;
    }

    public void addState(State s){
        states.add(s);
    }

    public void addParent(Variable newParent){
        parents.add(newParent);
    }

    public boolean hasParent(Variable var){
        for(Variable x : parents){
            if(x == var){
                return true;
            }
        }
        return false;
    }

    public boolean hasChild(Variable var) {
        for (Variable x : children) {
            if (x == var) {
                return true;
            }
        }
        return false;
    }

    public void addChild(Variable newChild){
        children.add(newChild);
    }

    public ArrayList<Variable> getParents(){
        return parents;
    }

    public ArrayList<Variable> getChildren(){
        return children;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 97 * hash + this.id;
        hash = 97 * hash + this.numberOfStates;
        return hash;
    }

    @Override public boolean equals(Object v){
        Variable var = (Variable)v;

        if((id == var.getId())&&(numberOfStates == var.getNumberOfStates())){
            return true;
        }
        else return false;
    }
    
    @Override public String toString(){
        StringBuilder result = new StringBuilder();
        String NEW_LINE = System.getProperty("line.separator");

        result.append("Variable ID: " + id + " ");
        result.append("Number of States: " + numberOfStates +"\n");

        return result.toString();
    }
    
}
